package com.sjd.web.controller.mall;

import com.alibaba.fastjson.JSONObject;
import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.core.page.TableDataInfo;
import com.sjd.mall.domain.SjdGoodsBannerOnlineOfflineRequest;
import com.sjd.mall.domain.dto.goods.*;
import com.sjd.mall.service.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Mr.s
 * @version : 1.0
 * @description : 商品controller
 * @date : 2022-01-20 22:43
 */
@RestController
@RequestMapping("mall/goods")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品
     *
     * @return return
     */
    @GetMapping("query")
    //@PreAuthorize("@ss.hasPermi('mall:goods:list')")
    public TableDataInfo queryGoods(SjdWebSiteGoodsQueryRequestDTO requestDTO) {
        startPage();
        List<SjdWebSiteGoodsQueryResponseDTO> sjdWebSiteGoodsQueryResponseDTOS = goodsService.queryGoodsWebSite(requestDTO);
        return getDataTable(sjdWebSiteGoodsQueryResponseDTOS);
    }

    /**
     * 查询商品 根据id查询接口
     *
     * @param goodsId goodsId
     * @return return
     */
    @GetMapping("query/{goodsId}")
    //@PreAuthorize("@ss.hasPermi('mall:goods:query')")
    public AjaxResult queryGoods(@PathVariable("goodsId") Long goodsId) {
        return goodsService.queryGoodsWebSiteByGoodsId(goodsId);
    }

    /**
     * 添加
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @PostMapping("add")
    //@PreAuthorize("@ss.hasPermi('mall:goods:add')")
    public AjaxResult addGoods(@Validated @RequestBody SjdGoodsAddRequestDTO requestDTO) {
        requestDTO.setCreatorId(getUserId());
        return goodsService.addGoods(requestDTO);
    }

    /**
     * 更新
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @PostMapping("update")
    //@PreAuthorize("@ss.hasPermi('mall:goods:update')")
    public AjaxResult updateGoods(@Validated @RequestBody SjdGoodsUpdateRequestDTO requestDTO) {
        return goodsService.updateGoods(requestDTO);
    }

    /**
     * 上下线
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @PostMapping("online")
//    //@PreAuthorize("@ss.hasPermi('mall:goods:online')")
    public AjaxResult onlineOfflineGoods(@Validated @RequestBody SjdGoodsOnlineOfflineRequestDTO requestDTO) {
        return goodsService.onlineOfflineGoods(requestDTO.getGoodsId(), requestDTO.getIsOnline());
    }

    /**
     * 删除商品
     *
     * @param goodsId goodsId
     * @return return
     */
    @PostMapping("delete/{goodsId}")
    //@PreAuthorize("@ss.hasPermi('mall:goods:delete')")
    public AjaxResult deleteGoods(@PathVariable("goodsId") Long goodsId) {
        return goodsService.deleteGoods(goodsId);
    }

    /**
     * 获取banner string
     *
     * @return return
     */
    @GetMapping("banner")
//    //@PreAuthorize("@ss.hasPermi('mall:banner:list')")
    public TableDataInfo getBanner() {
        List<JSONObject> banner = goodsService.getBanner();
        return getDataTable(banner);
    }


    /**
     * 上下线banner
     * @param request request
     * @return return
     */
    @PostMapping("banner/set")
    //    //@PreAuthorize("@ss.hasPermi('mall:banner:set')")
    public AjaxResult addBanner(@RequestBody @Validated SjdGoodsBannerOnlineOfflineRequest request) {
        if (request.getIsAdd()) {
            return goodsService.addBanner(request.getGoodsId());
        } else {
            return goodsService.removeBanner(request.getGoodsId());
        }
    }
}
